package com.holly.demo.modular.com.holly.unit.dict.modular.entity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.holly.unit.db.api.factory.PageFactory;
import com.holly.unit.db.api.factory.PageResultFactory;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.dict.modular.entity.HollyFormType;
import com.holly.unit.dict.modular.entity.HollyFormTypeRequest;
import com.holly.unit.dict.modular.mapper.HollyFormTypeMapper;
import com.holly.unit.dict.modular.service.IHollyFormTypeService;
import com.holly.unit.office.api.OfficeExcelApi;
import com.holly.unit.office.api.pojo.report.ExcelExportParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 表单分类
 * @Author: holly
 * @Date:   2022-02-23
 * @Version: V1.0
 */
@Service
public class HollyFormTypeServiceImpl extends ServiceImpl<HollyFormTypeMapper, HollyFormType> implements IHollyFormTypeService {

    @Resource
    private IHollyFormTypeService hollyFormTypeService;

    @Resource
    private OfficeExcelApi officeExcelApi;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(HollyFormTypeRequest hollyFormTypeRequest) {
        HollyFormType hollyFormType = new HollyFormType();
        BeanUtil.copyProperties(hollyFormTypeRequest, hollyFormType);
        this.save(hollyFormType);
    }

    @Override
    public void del(HollyFormTypeRequest hollyFormTypeRequest) {
        this.removeById(hollyFormTypeRequest.getId());
    }

    @Override
    public void edit(HollyFormTypeRequest hollyFormTypeRequest) {
        HollyFormType hollyFormType = new HollyFormType();
        BeanUtil.copyProperties(hollyFormTypeRequest, hollyFormType);
        this.updateById(hollyFormType);
    }

    @Override
    public HollyFormType detail(HollyFormTypeRequest hollyFormTypeRequest) {
        return this.getById(hollyFormTypeRequest.getId());
    }

    @Override
    public List<HollyFormType> findList(HollyFormTypeRequest hollyFormTypeRequest) {
        return this.list(this.createWrapper(hollyFormTypeRequest));
    }

    @Override
    public PageResult<HollyFormType> findPage(HollyFormTypeRequest hollyFormTypeRequest) {
        Page<HollyFormType> page = this.page(PageFactory.defaultPage(), this.createWrapper(hollyFormTypeRequest));
        return PageResultFactory.createPageResult(page);
    }


    /**
     * 构建wrapper
     *
     * @author holly
     * @date 2021/1/13 10:50
     */
    private LambdaQueryWrapper<HollyFormType> createWrapper(HollyFormTypeRequest hollyFormTypeRequest) {
        LambdaQueryWrapper<HollyFormType> queryWrapper = new LambdaQueryWrapper<>();

        // SQL拼接
         //queryWrapper.eq(ObjectUtil.isNotNull(dictRequest.getDictId()), SysDict::getDictId, dictRequest.getDictId());
        queryWrapper.eq(ObjectUtil.isNotNull(hollyFormTypeRequest.getRemark()), HollyFormType::getRemark, hollyFormTypeRequest.getRemark());
        queryWrapper.eq(ObjectUtil.isNotNull(hollyFormTypeRequest.getSort()), HollyFormType::getSort, hollyFormTypeRequest.getSort());
        queryWrapper.eq(ObjectUtil.isNotNull(hollyFormTypeRequest.getTypeCode()), HollyFormType::getTypeCode, hollyFormTypeRequest.getTypeCode());
        queryWrapper.eq(ObjectUtil.isNotNull(hollyFormTypeRequest.getTypeName()), HollyFormType::getTypeName, hollyFormTypeRequest.getTypeName());
        queryWrapper.eq(ObjectUtil.isNotNull(hollyFormTypeRequest.getId()), HollyFormType::getId, hollyFormTypeRequest.getId());
        return queryWrapper;
    }
     /**
         * 导出用户
         *
         * @param response httpResponse
         * @author holly
         * @Date:   2022-02-23
         */
        @Override
        public void export(HttpServletResponse response) {
            ExcelExportParam excelExportParam = new ExcelExportParam();
            List<HollyFormType> hollyFormTypeList = this.list();

            excelExportParam.setClazz(HollyFormType.class);
            excelExportParam.setDataList(hollyFormTypeList);
            excelExportParam.setExcelTypeEnum(ExcelTypeEnum.XLS);
            excelExportParam.setFileName("系统用户导出");
            excelExportParam.setResponse(response);

            officeExcelApi.easyExportDownload(excelExportParam);
        }

}
